﻿// --------------------------------
// <copyright file="Label.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>9. 11. 2011 22:22:37</date>

// <summary>
//
// </summary>
// --------------------------------

#if WINDOWS

using FITClub.Episode1.Levels;
using System.Drawing.Design;
using System.ComponentModel;
using FITClub.TextureMenu.Configuration;

#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FITClub.TextureMenu
{
#if WINDOWS

    [System.Serializable]
#endif
    public enum TextAlignment
    {
        Left, Center, Right
    }

    /// <summary>
    /// Displays text.
    /// </summary>
#if WINDOWS

    [System.Serializable]
#endif
    public class Label : ADisplayable
    {
        [XmlAttribute]
#if WINDOWS
        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string FontPath { get; set; }

        [XmlAttribute]
#if WINDOWS
        [Category("Design")]
#endif
        public bool DrawBackground { get; set; }

        [XmlAttribute]
#if WINDOWS
        [Category("Design")]
#endif
        public TextAlignment Alignment { get; set; }

#if WINDOWS

        [Category("Content")]
#endif
        [XmlElement]
        public string Text { get; set; }

        [XmlElement]
#if WINDOWS
        [Category("Colors")]
        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public Color TextColor { get; set; }

        [XmlElement]
#if WINDOWS
        [Category("Colors")]
        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public Color Background { get; set; }

#if WINDOWS
        [System.NonSerialized]
#endif
        private SpriteFont font;
#if WINDOWS
        [System.NonSerialized]
#endif
        private Texture2D texture;

        public Label()
            : base()
        {
        }

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            if (this.ContentLoaded == false)
            {
                this.font = MenuHelper.LoadFontOrDefault(loader, this.FontPath);
                texture = MenuHelper.CreateFillTexture(this.Background);

                this.ContentLoaded = true;
            }
        }

        public override void Dispose()
        {
            if (this.ContentLoaded)
            {
                this.texture.Dispose();
            }
            this.ContentLoaded = false;
        }

        public override void Draw(SpriteBatch spriteBatch, Color tint)
        {
            if (this.DrawBackground)
            {
                spriteBatch.Draw(this.texture, this.Bounds, tint);
            }
            MenuHelper.DrawText(spriteBatch, this.font, this.Text, this.TextColor, this.Bounds, this.Alignment, this.Resizable);
        }
    }
}